package com.sos.service.impl;

import com.sos.exception.CustomException;
import com.sos.exception.ErrorCode;
import com.sos.model.dto.HealthProfileDTO;
import com.sos.model.dto.HealthProfileUpdateDTO;
import com.sos.model.entity.HealthProfile;
import com.sos.model.entity.User;
import com.sos.model.vo.ResultVO;
import com.sos.repository.HealthProfileRepository;
import com.sos.repository.UserRepository;
import com.sos.service.HealthProfileService;
import com.sos.util.UserContextUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class HealthProfileServiceImpl implements HealthProfileService {

    private final HealthProfileRepository healthProfileRepository;
    private final UserRepository userRepository;
    private final UserContextUtils userContextUtils;

    @Override
    @Transactional
    public ResultVO<HealthProfileDTO> saveOrUpdateHealthProfile(HealthProfileUpdateDTO healthProfileUpdateDTO) {
        // 1. 获取当前用户
        Long userId = userContextUtils.getCurrentUserId();

        // 2. 查找用户
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

        // 3. 查找或创建健康档案
        HealthProfile healthProfile = healthProfileRepository.findByUserId(userId)
                .orElse(new HealthProfile());

        // 4. 更新健康档案信息
        healthProfile.setUserId(userId);
        healthProfile.setBloodType(healthProfileUpdateDTO.getBloodType());
        healthProfile.setAllergies(healthProfileUpdateDTO.getAllergies());
        healthProfile.setMedicalConditions(healthProfileUpdateDTO.getMedicalConditions());
        healthProfile.setMedications(healthProfileUpdateDTO.getMedications());
        healthProfile.setEmergencyContactName(healthProfileUpdateDTO.getEmergencyContactName());
        healthProfile.setEmergencyContactPhone(healthProfileUpdateDTO.getEmergencyContactPhone());
        healthProfile.setHeight(healthProfileUpdateDTO.getHeight());
        healthProfile.setWeight(healthProfileUpdateDTO.getWeight());
        healthProfile.setAdditionalInfo(healthProfileUpdateDTO.getAdditionalInfo());

        // 5. 保存健康档案
        HealthProfile savedHealthProfile = healthProfileRepository.save(healthProfile);

        // 6. 转换为DTO并返回
        HealthProfileDTO healthProfileDTO = convertToDTO(savedHealthProfile);
        return ResultVO.success(healthProfileDTO);
    }

    @Override
    public ResultVO<HealthProfileDTO> getCurrentUserHealthProfile() {
        // 1. 获取当前用户
        Long userId = userContextUtils.getCurrentUserId();

        // 2. 查找健康档案
        HealthProfile healthProfile = healthProfileRepository.findByUserId(userId)
                .orElseThrow(() -> new CustomException(ErrorCode.HEALTH_PROFILE_NOT_FOUND));

        // 3. 转换为DTO并返回
        HealthProfileDTO healthProfileDTO = convertToDTO(healthProfile);
        return ResultVO.success(healthProfileDTO);
    }

    @Override
    public ResultVO<HealthProfileDTO> getHealthProfileByUserId(Long userId) {
        // 1. 查找用户
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

        // 2. 查找健康档案
        HealthProfile healthProfile = healthProfileRepository.findByUserId(userId)
                .orElseThrow(() -> new CustomException(ErrorCode.HEALTH_PROFILE_NOT_FOUND));

        // 3. 转换为DTO并返回
        HealthProfileDTO healthProfileDTO = convertToDTO(healthProfile);
        return ResultVO.success(healthProfileDTO);
    }

    /**
     * 将HealthProfile实体转换为HealthProfileDTO
     */
    private HealthProfileDTO convertToDTO(HealthProfile healthProfile) {
        HealthProfileDTO dto = new HealthProfileDTO();
        BeanUtils.copyProperties(healthProfile, dto);
        return dto;
    }
}